package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ChooseRoleAdp;
import com.baihan.angus.app.game.common.bean.response.ScriptRole;
import com.baihan.angus.app.game.common.data_center.cache.GameCache;

import java.util.List;

public class ChooseRoleDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;
    private ImageView ivClose;
    private TextView tvTitle, tvSend;
    private RecyclerView rvRole;

    private ChooseRoleAdp mChooseRoleAdp;
    private int dialogType;
    private int multi;
    private List<ScriptRole> dataList;

    public ChooseRoleDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = AppUtil.getScreenHeight(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(R.color.dialog_bg);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_select_role, (ViewGroup) null);
        setContentView(dialogView);
        rvRole = dialogView.findViewById(R.id.rvRole);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        tvSend = dialogView.findViewById(R.id.tvSend);
        ivClose = dialogView.findViewById(R.id.ivClose);

        tvSend.setOnClickListener(v -> {
            if (null != mCallback) {
                StringBuilder ids = new StringBuilder();
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).isSelected()) {
                        ids.append(dataList.get(i).getScript_role_id()).append(",");
                    }
                }
                if (!TextUtils.isEmpty(ids.toString())) {
                    String roleIds = ids.substring(0, ids.length() - 1);
                    mCallback.send(dialogType, roleIds);
                } else {
                    Tooast.normalInfo("请选择角色");
                }
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initData(int type, List<ScriptRole> roleList, int isAll, int isMulti) {
        dialogType = type;
        dataList = roleList;
        multi = isMulti;
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i).isSelected()) {
                dataList.get(i).setSelected(false);
            }
        }
        if (isAll == 0) {
            for (int i = 0; i < dataList.size(); i++) {
                if (dataList.get(i).getScript_role_id().equals(GameCache.chooseRoleId)) {
                    dataList.remove(dataList.get(i));
                }
            }
        }

        mChooseRoleAdp = new ChooseRoleAdp(mContext, dataList, type, multi);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        rvRole.setLayoutManager(gridLayoutManager);
        mChooseRoleAdp.bindRecycleView(rvRole);

        if (type == 0) {
            tvTitle.setText("请选择要透露线索的角色");
            tvSend.setText("透露");
        } else if (type == 1) {
            tvTitle.setText("请选择要转交道具的角色");
            tvSend.setText("转交");
        } else if (type == 2) {
            tvTitle.setText("请选择道具使用角色");
            tvSend.setText("使用");
        }
    }

    public interface Callback {
        void send(int type, String roleIds);
    }
}
